"""
Case Type   : cm
Case Name   : cm_ctl stop指定停止模式参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、cm_ctl stop停止AZ所有实例，指定模式为smart
    3、cm_ctl stop停止单个主机的所有实例，指定模式为fast
    4、cm_ctl stop单独停止一个实例进程(dn),指定模式为immediate
    5、恢复集群
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、停止成功
    3、停止成功
    4、停止成功
    5、恢复集群成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:cm_ctl stop停止AZ所有实例，指定模式为smart; expect:停止成功----'
        self.log.info(text)

        stop1 = self.comsh.exec_cm_ctl('stop', '-m fast')
        self.log.info(stop1)
        self.assertIn(self.constant.cm_stop_success_msg, stop1, '停止实例失败')

        text = '----step3:cm_ctl stop停止单个主机的所有实例，指定模式为fast  expect:停止成功----'
        self.log.info(text)

        self.log.info('-----拉起集群-----')
        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        stop2 = self.comsh.exec_cm_ctl('stop', '-n 1', '-m fast')
        self.log.info(stop2)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop2, '停止失败')

        text = '----step4:cm_ctl switch rollback回滚  expect:操作成功----'
        self.log.info(text)

        self.log.info('-----拉起集群-----')
        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        stop3 = self.comsh.exec_cm_ctl('stop', '-n 2',
                                       f'-D {macro.DB_INSTANCE_PATH} -m immediate')
        self.log.info(stop3)
        self.assertIn(self.constant.cm_stop_instance_success_msg, stop3, '停止失败')

    def tearDown(self):
        text = '----step5:恢复集群状态  expect:恢复集群初始状态成功----'
        self.log.info(text)

        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        p_status = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        if 'Primary' in p_status:
            self.log.info('---集群状态正常，无需恢复为初始状态---')
        else:
            recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
            self.log.info(recovery)

            self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                          '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
